﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using TPOJ.IDAL;
using TPOJ.Model;

namespace TPOJ.LINQDAL
{
    public class ForumDAO : IForumDAO
    {
        public IList<ForumMessage> GetForumMessagesByProblemId(int problemId)
        {
            IList<ForumMessage> result = new List<ForumMessage>();

            using (var entities = new TPOJEntities())
            {
                var query = from m in entities.ForumMessages
                            where m.ProblemId == problemId && m.Defunct == false
                            select m;
                foreach (var message in query)
                {
                    result.Add(message);
                }
            }
            return result;
        }

        public void Create(ForumMessage message)
        {
            using (var entities = new TPOJEntities())
            {
                entities.AddToForumMessages(message);
                entities.SaveChanges();
            }
        }
        
        public ForumMessage GetForumMessage(int id)
        {
            ForumMessage result;

            using (var entities = new TPOJEntities())
            {
                var query = from m in entities.ForumMessages
                            where m.UniqueId == id
                            select m;
                result = query.First();
                if (result.Defunct == true)
                {
                    result.Title = "This message is blocked by administrator.";
                    result.MessageContent = "Please Contact the site administrator for more infomation.";
                }
            }
            return result;
        }
    }
}
